package com.nl.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class StringUtil {
	public StringUtil() {
	}

	public static ArrayList<String> getStepVars(String str, int step) {
		ArrayList<String> vars = new ArrayList<String>();
		if (str == null) {
			return vars;
		}
		for (int j = 1; j < step; j++){
			if (str.lastIndexOf((new StringBuilder()).append("S:").append(j).append(" ").toString()) >= 0) {
				vars.add((new StringBuilder()).append("S_").append(j).toString());
			}
		}
		return vars;
	}

	public static List<String> getAllVars(String str) {
		ArrayList<String> vars = new ArrayList<String>();
		String tmpStr = str;
		if (tmpStr == null){
			return vars;
		}
		do {
			if (!tmpStr.contains("${") || tmpStr.indexOf("}") <= 0 || tmpStr.indexOf("}") <= tmpStr.indexOf("${")){
				break;
			}
			int idxStart = tmpStr.indexOf("${");
			int idxEnd = tmpStr.indexOf("}");
			vars.add(tmpStr.substring(idxStart, idxEnd + 1));
			if (idxEnd >= tmpStr.length() - 1){
				break;
			}
			tmpStr = tmpStr.substring(idxEnd + 1);
		} while (true);
		return vars;
	}

	public static String join(List<String> list, String delim) {
		if (list == null || list.size() < 1){
			return null;
		}
		StringBuilder buf = new StringBuilder();
		Iterator<String> i = list.iterator();
		do {
			if (!i.hasNext()){
				break;
			}
			buf.append((String) i.next());
			if (i.hasNext()){
				buf.append(delim);
			}
		} while (true);
		return buf.toString();
	}

	public static List<String> split(String str, int delim) {
		List<String> splitList = null;
		if (str == null){
			return null;
		}
		String l = "";
		int pre = 0;
		int len = str.length();
		for (int i = 0; i < len; i++) {
			if (str.charAt(i) != delim){
				continue;
			}
			l = str.substring(pre, i);
			if (splitList == null){
				splitList = new ArrayList<String>();
			}
			splitList.add(l);
			pre = i + 1;
		}

		if (pre != len) {
			l = str.substring(pre, len);
			splitList.add(l);
		}
		return splitList;
	}

	public static List<String> split(String str, char delim) {
		List<String> splitList = null;
		if (str == null){
			return null;
		}
		if (str.equals("")){
			return null;
		}
		String l = "";
		int pre = 0;
		int len = str.length();
		for (int i = 0; i < len; i++) {
			if (str.charAt(i) != delim){
				continue;
			}
			l = str.substring(pre, i);
			if (splitList == null){
				splitList = new ArrayList<String>();
			}
			splitList.add(l);
			pre = i + 1;
		}

		l = str.substring(pre, len);
		if (splitList == null){
			splitList = new ArrayList<String>();
		}
		splitList.add(l);
		return splitList;
	}

	public static List<String> split(String str, String delim) {
		if (delim.length() == 1){
			return split(str, delim.charAt(0));
		}
		List<String> splitList = null;
		StringTokenizer st = null;
		if (str == null){
			return null;
		}
		st = new StringTokenizer(str, delim);
		if (st.hasMoreTokens()) {
			splitList = new ArrayList<String>();
			for (; st.hasMoreTokens(); splitList.add(st.nextToken()));
		}
		return splitList;
	}

	public static String replace(String mainString, String oldString, String newString) {
		if (mainString == null){
			return null;
		}
		int i = mainString.lastIndexOf(oldString);
		if (i < 0){
			return mainString;
		}
		StringBuilder mainSb = new StringBuilder(mainString);
		for (; i >= 0; i = mainString.lastIndexOf(oldString, i - 1)){
			mainSb.replace(i, i + oldString.length(), newString);
		}

		return mainSb.toString();
	}

	public static String replace(String mainString, int oldString, String newString) {
		if (mainString == null){
			return null;
		}
		int i = mainString.lastIndexOf(oldString);
		if (i < 0){
			return mainString;
		}
		StringBuilder mainSb = new StringBuilder(mainString);
		for (; i >= 0; i = mainString.lastIndexOf(oldString, i - 1)){
			mainSb.replace(i, i + 1, newString);
		}
		return mainSb.toString();
	}

	public static boolean nullOrBlank(String param) {
		return param == null || param.trim().equals("");
	}

	public static int parseInt(String param) {
		int i = 0;
		try {
			i = Integer.parseInt(param);
		} catch (Exception e) {
			i = (int) parseFloat(param);
		}
		return i;
	}

	public static long parseLong(String param) {
		long l = 0L;
		try {
			l = Long.parseLong(param);
		} catch (Exception e) {
			l = (long) parseDouble(param);
		}
		return l;
	}

	public static float parseFloat(String param) {
		float f = 0.0F;
		try {
			f = Float.parseFloat(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}

	public static double parseDouble(String param) {
		double d = 0.0D;
		try {
			d = Double.parseDouble(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public static boolean parseBoolean(String param) {
		if (nullOrBlank(param)){
			return false;
		}
		switch (param.charAt(0)) {
		case 49: // '1'
		case 84: // 'T'
		case 89: // 'Y'
		case 116: // 't'
		case 121: // 'y'
			return true;
		}
		return false;
	}

	public static String convertGb2312(String input) {
		String str = input;
		try {
			str = new String(input.getBytes("ISO8859_1"), "gb2312");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String isInteger(String input) {
		String ret = "1";
		int i = 0;
		do {
			if (i >= input.length()){
				break;
			}
			if (input.charAt(i) < '0' || input.charAt(i) > '9') {
				ret = "0";
				break;
			}
			i++;
		} while (true);
		return ret;
	}

	public static String Md5(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte byteArray[] = messageDigest.digest();
		StringBuilder md5StrBuff = new StringBuilder();
		byte arr$[] = byteArray;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			byte aByteArray = arr$[i$];
			if (Integer.toHexString(255 & aByteArray).length() == 1){
				md5StrBuff.append("0").append(
						Integer.toHexString(255 & aByteArray));
			} else {
				md5StrBuff.append(Integer.toHexString(255 & aByteArray));
			}
		}

		return md5StrBuff.toString().toUpperCase();
	}
	public int lastIndex(String str,String ch) {
	    
        return 0;
	    
	}
}
